<?php
namespace application\index\controller;
use application\common\model\ExamineeInfo;
use think\Controller;
use application\common\controller\Base;
use think\exception\HttpException;
use think\Session;
use think\View;
use think\Url;
use think\Request;
use application\common\model\User;
use application\common\model\SystemConfig;

class Index extends Controller
{
    public function index()
    {
        $this->redirect('login');
    }
    public function login() {
        $user = new User();
        if(! $this->request->has('userType','post')) {
            //throw new HttpException('500');
            $this->assign([
                'title' => '登录'
            ]);
            return $this->fetch();
        }else {
            $userType = $this->request->post('userType');
            if ($userType == '3') {
                $studentID = $this->request->post('studentID');
                $studentName = $this->request->post('studentName');
                $examineeInfo = new ExamineeInfo();
                $userInfo = $examineeInfo->where(['studentID' => $studentID, 'studentName' => $studentName])->select();
            }else {
                $userName = $this->request->post('userName');
                $userPassword = $this->request->post('userPassword');
                $userInfo = $user->where([
                    'user_name' => $userName,
                    'user_password' => md5($userPassword),
                    'user_role' => $userType,
                ])->select();
            }
            if ($userInfo) {
                if ($userType == '3') {
                    Session::set('studentID',$studentID);
                    Session::set('userName',$studentName);
                }else {
                    Session::set('userName',$userName);
                    Session::set('userId',$userInfo[0]['user_id']);
                }
                $config = new SystemConfig();
                $configInfo = $config->where('system_id', '1')->select()[0];
                switch ($userType) {
                    case '1':
                        if ($userName == 'admin') {
                            $other = $user->where('user_role',1)->where('user_name','<>','admin')->select();
                            if ($other) {
                                Session::destroy();
                                $this->error("请使用其他管理员账号登陆！",'login');
                            }else {
                                Session::set('pageNum', $configInfo['perPageNum']);
                                $this->redirect('admin/index/index');
                            }
                        }else {
                            Session::set('pageNum', $configInfo['perPageNum']);
                            $this->redirect('admin/index/index');
                        }
                        break;
                    case '2':
                        Session::set('pageNum', $configInfo['perPageNum']);
                        $this->redirect('teacher/index/index');
                        break;
                    case '3':
                        Session::set('pageNum', $configInfo['perPageNum']);
                        $this->redirect('student/index/index');
                        break;
                    default:
                        $this->error('登录失败，请确认您的账户密码是否正确！','login');
                        break;
                }
            }else {
                $this->error('登录失败，请确认您的账户密码是否正确！','login');
            }
        }
    }
}